"""
@Project :auto-phone
@File    :spiderUtils.py
@IDE     :PyCharm
@Author  :zbb
@Date    :2024/2/29 14:28
"""
import requests


class Playwright2Session:

    def __init__(self, driver, session):
        self.driver = driver
        self.session = session

    def get_session(self):
        cookie_list = self.driver.context.cookies()
        cookie_dict = {}
        for cookie in cookie_list:
            cookie_dict[cookie['name']] = cookie['value']
        self.session.cookies.update(cookie_dict)
        return self.session

    def get_driver(self):
        cookie_dict = requests.utils.dict_from_cookiejar(self.session.cookies)
        self.driver.context.add_cookies(cookie_dict)
        return self.driver


class Selenium2Session:

    def __init__(self, driver, session):
        self.driver = driver
        self.session = session

    def get_session(self):
        cookie_list = self.driver.get_cookies()
        cookie_dict = {}
        for cookie in cookie_list:
            cookie_dict[cookie['name']] = cookie['value']
        self.session.cookies.update(cookie_dict)
        return self.session

    def get_driver(self):
        cookie_dict = requests.utils.dict_from_cookiejar(self.session.cookies)
        self.driver.add_cookie(cookie_dict)
        return self.driver
